package com.hongxun.budget.ui.activity.sg;

import android.Manifest;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.hongxun.budget.R;
import com.hongxun.budget.ui.activity.BaseActivity;
import com.hongxun.budget.ui.dialog.ListDialog;
import com.hongxun.budget.ui.fragment.BaseFragment;
import com.hongxun.budget.ui.fragment.sg.MaterialPresentDumpsMap;
import com.hongxun.budget.ui.fragment.sg.MaterialPresentRecordList;
import com.hongxun.budget.ui.fragment.sg.MiddleCheckList;
import com.hongxun.budget.ui.fragment.sg.MiddleCheckMap;
import com.hongxun.budget.ui.fragment.sg.ProjectProgressList;
import com.hongxun.budget.utils.DateUtils;
import com.hongxun.budget.utils.GreenDaoManager;
import com.hongxun.budget.utils.SPUtils;
import com.hongxun.budget.utils.SgDBUtils;
import com.hongxun.budget.utils.SgStrKey;
import com.hongxun.budget.utils.SgXUtils;
import com.hongxun.budget.utils.T;
import com.hongxun.greenentity.sg.CPBaseEntity;
import com.hongxun.greenentity.sg.CScheduleEntity;
import com.hongxun.interfaces.ResponseListener;
import com.hongxun.net.SgProjectNet;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import kr.co.namee.permissiongen.PermissionFail;
import kr.co.namee.permissiongen.PermissionGen;
import kr.co.namee.permissiongen.PermissionSuccess;

/**
 * 项目进度2级界面
 * 材料到场2级界面
 * 中间检查2级界面
 */
public class ConstructionTwoActivity extends BaseActivity {
    @BindView(R.id.center_title)
    TextView centerTitle;
    @BindView(R.id.right_btn)
    Button rightBtn;
    @BindView(R.id.right_save)
    TextView rightSave;

    private String tabName;
    private BaseFragment baseFragment;
    private ProjectProgressList progressTwo;
    private MaterialPresentRecordList recordList;
    private MaterialPresentDumpsMap recordMap;
    private MiddleCheckList checkList;
    private MiddleCheckMap checkMap;

    private List<String> stringList = new ArrayList<>();
    private List<CScheduleEntity> entities = new ArrayList<>();

    @Override
    public int getLayoutId() {
        return R.layout.sg_activity_flayout;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        rightBtn.setVisibility(View.VISIBLE);
        rightSave.setVisibility(View.VISIBLE);
        rightSave.setTextColor(getResources().getColor(R.color.colorAccent));
        rightBtn.setText("同步");
        centerTitle.setText(String.valueOf(SPUtils.get(context, SgStrKey.namexm, "")));
        tabName = getIntent().getStringExtra("tabName");
        changeTab(tabName);
        entities.addAll(SgDBUtils.getScheduleAll(String.valueOf(SPUtils.get(context, SgStrKey.uuidxm, ""))));
        for (CScheduleEntity entity : entities) {
            stringList.add(entity.getSName());
        }
    }

    private void changeTab(String tab) {
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        if (baseFragment != null) transaction.hide(baseFragment);
        switch (tab) {
            case SgStrKey.xmjd:
                rightSave.setText("采集");
                if (progressTwo == null) {
                    progressTwo = new ProjectProgressList();
                    transaction.add(R.id.flayout1, progressTwo, tabName).commit();
                } else {
                    transaction.show(progressTwo).commit();
                    progressTwo.onRefresh();
                }
                baseFragment = progressTwo;
                break;
            case SgStrKey.cldclist:
                tabName = SgStrKey.cldcmap;
                rightSave.setText("地图");
                if (recordList == null) {
                    recordList = new MaterialPresentRecordList();
                    transaction.add(R.id.flayout1, recordList, tabName).commit();
                } else {
                    transaction.show(recordList).commit();
                    recordList.onRefresh();
                }
                baseFragment = recordList;
                break;
            case SgStrKey.cldcmap:
                tabName = SgStrKey.cldclist;
                rightSave.setText("列表");
                if (recordMap == null) {
                    recordMap = new MaterialPresentDumpsMap();
                    transaction.add(R.id.flayout1, recordMap, tabName).commit();
                } else {
                    transaction.show(recordMap).commit();
                    recordMap.onRefresh();
                }
                baseFragment = recordMap;
                break;
            case SgStrKey.zjjclist:
                tabName = SgStrKey.zjjcmap;
                rightSave.setText("地图");
                if (checkList == null) {
                    checkList = new MiddleCheckList();
                    transaction.add(R.id.flayout1, checkList, tabName).commit();
                } else {
                    transaction.show(checkList).commit();
                    checkList.onRefresh();
                }
                baseFragment = checkList;
                break;
            case SgStrKey.zjjcmap:
                tabName = SgStrKey.zjjclist;
                rightSave.setText("列表");
                if (checkMap == null) {
                    checkMap = new MiddleCheckMap();
                    transaction.add(R.id.flayout1, checkMap, tabName).commit();
                } else {
                    checkMap.initDault();
                    transaction.show(checkMap).commit();
                }
                baseFragment = checkMap;
                break;
        }
    }

    @OnClick({R.id.right_btn, R.id.right_save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.right_btn:
                SgProjectNet.getProjectState(context, this, 0);
                break;
            case R.id.right_save:
                rightClick();
                break;
        }
    }

    //右边按钮点击
    private void rightClick() {
        switch (tabName) {
            case SgStrKey.xmjd:
                showDialogAdd();
                break;
            case SgStrKey.cldclist:
            case SgStrKey.cldcmap:
            case SgStrKey.zjjclist:
            case SgStrKey.zjjcmap:
                changeTab(tabName);
                break;
        }
    }

    //数据同步
    private void syncData() {
        switch (tabName) {
            case SgStrKey.xmjd:
                SgProjectNet.upProjectProgress(context, this, 1);
                break;
            case SgStrKey.cldclist:
            case SgStrKey.cldcmap:
                SgProjectNet.upProjectPresent(context, this, 11);
                break;
            case SgStrKey.zjjclist:
            case SgStrKey.zjjcmap:
                SgProjectNet.upProjectMiddleCheck(context, this, 21);
                break;
        }
    }

    //新增记录
    private void showDialogAdd() {
        new ListDialog<String>()
                .setTitle("选择项目进度")
                .setDatas(stringList)
                .setListener(new ResponseListener() {
                    @Override
                    public void response(Object... data) {
                        SPUtils.put(context, SgStrKey.namejd, data[0]);
                        SPUtils.put(context, SgStrKey.uuidjdjl, "");
                        for (CScheduleEntity entity : entities) {
                            if (entity.getSName().equals(data[0])) {
                                SPUtils.put(context, SgStrKey.dpgqtime, DateUtils.getDateToStringNoHH(entity.getInverTime()));
                                break;
                            }
                        }
                        startActivity(new Intent(context, ScheduleReportAddActivity.class)
                                .putExtra("isNewAdd", true));
                    }
                }).show(getFragmentManager(), "");
    }

    @Override
    public void err(int i, int status, String err) {
        if (status == 11 || status == 22) {//11归档，22删除
            T.showShort(context, err);
            String uuid = String.valueOf(SPUtils.get(context, SgStrKey.uuidxm, ""));
            List<CPBaseEntity> list2 = SgDBUtils.getCPBaseList(false);
            for (CPBaseEntity entity : list2) {
                if (entity.getGuid().equals(uuid)) {
                    entity.setPBDatastate(SgStrKey.del);
                    GreenDaoManager.getInstance().getmDaoSession()
                            .getCPBaseEntityDao()
                            .update(entity);
                    break;
                }
            }
            finish();
        } else {
            super.err(i, status, err);
        }
    }

    @Override
    public void success(int i, JSONObject obj) {
        switch (i) {
            case 0:
                syncData();
                break;
            case 1:
                SgProjectNet.getProjectProgress(context, this, 2);
                break;
            case 2:
                SgXUtils.dealProjectProgress(context, obj, this, 3);
                break;
            case 3:
                SgProjectNet.upPhotoSgAllPart(context, this, 4);
                break;
            case 4:
                SgProjectNet.getPhotoSg(context, this, 5, 1);
                break;
            case 5:
                SgXUtils.dealProjectPhotos(context, obj, this, 6);
                break;
            case 6:
                progressTwo.onRefresh();
                break;
            case 11:
                SgProjectNet.getProjectPresent(context, this, 12);
                break;
            case 12:
                SgXUtils.dealProjectPresent(context, obj, this, 13);
                break;
            case 13:
                SgProjectNet.upPhotoSgAllPart(context, this, 14);
                break;
            case 14:
                SgProjectNet.getPhotoSg(context, this, 15, 2);
                break;
            case 15:
                SgXUtils.dealProjectPhotos(context, obj, this, 16);
                break;
            case 16:
                recordList.onRefresh();
                break;
            case 21:
                SgProjectNet.getProjectMiddleCheck(context, this, 22);
                break;
            case 22:
                SgXUtils.dealProjectMiddleCheck(context, obj, this, 23);
                break;
            case 23:
                SgProjectNet.upPhotoSgAllPart(context, this, 24);
                break;
            case 24:
                SgProjectNet.getPhotoSg(context, this, 25, 3);
                break;
            case 25:
                SgXUtils.dealProjectPhotos(context, obj, this, 26);
                break;
            case 26:
                checkList.onRefresh();
                break;
        }
    }
}
